//
//  XXTGSearchAddressView.m
//  XiuXiuTuanGou
//
//  Created by 张敬 on 2019/4/4.
//  Copyright © 2019年 XiuXiuTuanGou. All rights reserved.
//

#import "MeatSearchAddressView.h"

#define selfH 45
#define cancelBtnW 44


@interface MeatSearchAddressView()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *cancelBtn; //取消按钮
@property (nonatomic, strong) UIView *navView;

@property (nonatomic, strong) UITapGestureRecognizer *tap;


@end

@implementation MeatSearchAddressView

- (void)setupUI
{
    [self addSubview:self.navView];
    [self addSubview:self.cancelBtn];
}


- (void)setupLayout
{
    self.height = 45;
    
    {
        _navView.mj_x = 15;
        _navView.mj_y = 0 ;
        _navView.size = CGSizeMake(KScreenWidth - 30 , 34);
    }
    
    {
        _textField.width = _navView.width - 36;
    }
    
    {
        //取消按钮
        _cancelBtn.size = CGSizeMake(cancelBtnW, _navView.height);
        _cancelBtn.mj_x = KScreenWidth - _cancelBtn.width - [self fitSize:8];
        _cancelBtn.mj_y = 0;

    }
    
}



#pragma mark - response methods

- (void)didClickedCancelButton
{
    [self cancelDealTextField];
}

//开始编辑
- (void)startDealTextField
{
    {
        _isEditState = YES;
        
        //取消按钮
        _cancelBtn.hidden = NO;
        
        _navView.width = KScreenWidth - 30 - cancelBtnW;
        _textField.width = _navView.width - 36;

    }
    if (self.startBlock) {
        self.startBlock();
    }
}

//结束编辑
- (void)cancelDealTextField
{
    {
        _isEditState = NO;
        _cancelBtn.hidden = YES;
        
        _navView.width = KScreenWidth - 30;
        _textField.width = _navView.width - 36;
        _textField.text = @"";
        [_textField resignFirstResponder];

    }
    if (self.endBlock) {
        self.endBlock();
    }
}

//MARK: - textField.delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self startDealTextField];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_textField resignFirstResponder];
    return YES;
}

- (void)textFieldChanged:(UITextField *)textField
{
    
}

#pragma mark - setter and getter

- (UITextField *)textField
{
    if (!_textField)
    {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(34, 0, 0, 34)];
        _textField.placeholder = @"输入您的收货地址";
        _textField.delegate = self;
        _textField.textColor = UIColorFromRGB(0xB2B2B2);
        _textField.font = mFont(14);
        [_textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}


- (UIView *)navView
{
    if (!_navView) {
        _navView = [UIView makeViewBgColor:mBackgroudColor cornerRadius:17];
        
        UIImageView *searchImgView = [UIImageView makeImageByImageName:@"meat_buy_search" cornerRadius:0];
        
        searchImgView.size = CGSizeMake(18, 18);
        searchImgView.mj_x = (36 - searchImgView.width)/2.0;
        searchImgView.mj_y = (36 - searchImgView.height)/2.0;
        [_navView addSubview:searchImgView];
        
        [_navView addSubview:self.textField];
    }
    return _navView;
}


//取消按钮
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.titleLabel.font = mFont(14);
        _cancelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(didClickedCancelButton) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.hidden = YES;
    }
    return _cancelBtn;
}



@end
